package com.kcyu.sfhinvoice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kcyu.sfhinvoice.domain.FileDO;
import com.kcyu.sfhinvoice.domain.Invoice;
import com.kcyu.sfhinvoice.domain.InvoiceStruct;
import com.kcyu.sfhinvoice.service.FileService;
import com.kcyu.sfhinvoice.service.InvoiceService;
import com.kcyu.sfhinvoice.service.InvoiceStructService;
import com.kcyu.sfhinvoice.mapper.InvoiceStructMapper;
import com.kcyu.sfhinvoice.utils.BaiduOcrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.ParseException;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
* @author yujiancong
* @description 针对表【invoice_struct】的数据库操作Service实现
* @createDate 2024-03-07 11:32:39
*/
@Service
@Slf4j
public class InvoiceStructServiceImpl extends ServiceImpl<InvoiceStructMapper, InvoiceStruct>
    implements InvoiceStructService{

    @Autowired
    private FileService fileService;

    @Value("${file-storage-path}")
    private String assetPath;

    @Override
    @Async
    public void ocrInvoiceStruct(Serializable invoiceId) {
        // 获取发票信息
        Invoice invoice = new Invoice().selectById(invoiceId);
        // 获取发票文件
        QueryWrapper<FileDO> fileDOQueryWrapper = new QueryWrapper<>();
        fileDOQueryWrapper.eq("busi_key", invoice.getFileKey());
        List<FileDO> fileDOList = fileService.list(fileDOQueryWrapper);

        BaiduOcrUtils baiduOcrUtils = new BaiduOcrUtils();

        for (int i = 0; i < fileDOList.size(); i++) {
            FileDO fileDO = fileDOList.get(i);
            // 调用ocr接口识别发票信息
            // 识别成功后，将识别结果保存到数据库
            String filePath = fileDO.getFilePath();
            BaiduOcrUtils.FileType fileType = null;
            if(fileDO.getFileType().equals("pdf")) {
                fileType = BaiduOcrUtils.FileType.PDF;
            } else {
                fileType = BaiduOcrUtils.FileType.PIC;
            }
            InvoiceStruct invoiceStruct = null;
            try {
                invoiceStruct = baiduOcrUtils.ocrInvoice(filePath, fileType);
                invoiceStruct.setInvoiceId(invoice.getId());
                invoiceStruct.setFileId(fileDO.getId());
                log.info("已完成发票{}识别，正在保存识别结果到数据库...", invoice.getId());
                this.save(invoiceStruct);

                break;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public InvoiceStruct getInvoiceStruct(Serializable invoiceId) {
        Invoice invoice = new Invoice().selectById(invoiceId);
        QueryWrapper<InvoiceStruct> invoiceStructQueryWrapper = new QueryWrapper<>();
        invoiceStructQueryWrapper.eq("invoice_id", invoice.getId());
        return this.getOne(invoiceStructQueryWrapper);
    }
}




